class Solution:
    def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
        if len(matrix) == 1:
            return matrix[0]
        if len(matrix[0]) == 1:
            return [line[0] for line in matrix]
        if len(matrix) == 2:
            return matrix[0] + matrix[1][::-1]
        outer_res = matrix[0] + [line[-1] for line in matrix[1:-1]] + list(reversed(matrix[-1]))+ [line[0] for line in reversed(matrix[1:-1])]
        if len(matrix[0]) == 2:
            return outer_res
        inner_res = self.spiralOrder([line[1:-1] for line in matrix[1:-1]])
        return outer_res + inner_res
